package com.dtf.admin.base;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.dtf.admin.base.JqGridFilter.FilerRule;
import com.dtf.core.common.utils.Const;

public class JqGridUtils {
	
	private static JqGridUtils jqGridUtils = null;
	
	public static JqGridUtils getInstance(){
		if (jqGridUtils == null) {
			jqGridUtils = new JqGridUtils();
		}
		return jqGridUtils;
	}
	
	public JqGridFilter getJqGridFilter(String filters){
		JqGridFilter jqGridFilter = new JqGridFilter();
		if(Const.strIsEmpty(filters)){
			return jqGridFilter;
		}
		Map map = (Map) JSON.parse(filters);
		String groupOp = Const.getStrValue(map, "groupOp");
		List<Map<String,String>> rules =  (List<Map<String, String>>) map.get("rules");
		List<FilerRule> ruleList = new ArrayList<FilerRule>();
		for (Map<String, String> ruleMap : rules) {
			FilerRule rule = jqGridFilter.new FilerRule();
			rule.field = Const.getStrValue(ruleMap, "field");
			rule.op = Const.getStrValue(ruleMap, "op");
			rule.data = Const.getStrValue(ruleMap, "data");
			ruleList.add(rule);
		}
		jqGridFilter.setRules(ruleList);
		jqGridFilter.setGroupOp(groupOp);
		return jqGridFilter;
	}
}
